package com.spring1.parse;

import com.spring1.SpringUtil;
import com.spring1.factory.BeanFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParseXmlBean {
    static BeanFactory beanFactory = new BeanFactory();
    static Map<String, Element> elementMap = new HashMap<>();

    public static BeanFactory parseXmlBean(String filepath) {
        SAXReader reader = new SAXReader();
        try {
            InputStream stream = ParseXmlBean.class.getClassLoader().getResourceAsStream(filepath);
            Document document = reader.read(stream);
            Element rootElement = document.getRootElement();
            List<Element> entries = rootElement.elements("bean");
            // 这个map记录id和元素，由于ref的原因
            for (Element entry : entries) {
                String beanId = entry.attributeValue("id");
                elementMap.put(beanId, entry);
            }
            for (Element entry : entries) {
                parseObjectElement(entry);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return beanFactory;
    }

    private static String getMethodName(String name) {
        return "set" + SpringUtil.firstLetterUpper(name);
    }

    /**
     * 对Bean的元素进行解析
     */
    public static Object parseObjectElement(Element entry) {
        String beanId = entry.attributeValue("id");
        // 如果已经解析了，那么就不需要再解析
        if (beanFactory.getBean(beanId) != null) {
            return beanFactory.getBean(beanId);
        }
        String beanClass = entry.attributeValue("class");
        Class<?> bean = null;
        try {
            bean = Class.forName(beanClass);
            Object beanObject = bean.newInstance();
            for (Object subEntry : entry.elements("property")) {
                String name = ((Element) subEntry).attributeValue("name");
                String value = ((Element) subEntry).attributeValue("value");
                String dataType = ((Element) subEntry).attributeValue("dataType");
                String ref = ((Element) subEntry).attributeValue("ref");
                if (ref != null) {
                    Object object = beanFactory.getBean(ref); // 从工厂得到object
                    // 如果object没有(有可能定义在后面)，那么解析这个bean
                    if (object == null) {
                        Element element = elementMap.get(ref);
                        Object refObject = parseObjectElement(element);
                        Method method = bean.getMethod(getMethodName(refObject.getClass().getSimpleName()), new Class[]{refObject.getClass()});
                        method.invoke(beanObject, new Object[]{refObject});
                    } else {
                        Method method = bean.getMethod(getMethodName(name), new Class[]{object.getClass()});
                        method.invoke(beanObject, new Object[]{object});
                    }
                    beanFactory.setBean(beanId, beanObject);
                } else {
                    Method method;
                    if (dataType == null) {
                        method = bean.getMethod(getMethodName(name), new Class[]{String.class});
                        method.invoke(beanObject, new Object[]{value});
                    } else if (dataType.equals("int")) {
                        method = bean.getMethod(getMethodName(name), new Class[]{int.class});
                        method.invoke(beanObject, new Object[]{Integer.valueOf(value)});
                    }
                }
            }
            beanFactory.setBean(beanId, beanObject);
            return beanObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
